package com.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.dao.DepartmentDao;
import com.dao.PersonDao;
import com.po.Department;
import com.po.Person;
import com.util.DbUtil;

import oracle.net.aso.p;

public class PersonDaoImpl implements PersonDao {

	@Override
	public int addPerson(Person person) {
		String sql = "insert into person(id,pname,birthday, tel, departmentid, salay) values(personsep.Nextval,?,?,?,?,?)";
		Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		int m = 0;

		try {
			conn = DbUtil.getConnection();
			st = conn.prepareStatement(sql);

			// 改成自增长的
			st.setString(1, person.getpName());
			st.setDate(2, person.getBirthday());
			st.setInt(3, person.getTel());
			st.setInt(4, person.getDepartmentId());
			st.setDouble(5, person.getSalay());

			DepartmentDao dd = new DepartmentDaoImpl();
			if (dd.loadDepartment(person.getDepartmentId()) != null) {

				m = st.executeUpdate();
			} else {
				m = 0;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DbUtil.free(conn, st, rs);
		}

		return m;
	}

	@Override
	public int updatePerson(Person person) {
		String sql = "update person set pname=?,birthday=? ,tel=?, departmentId=?, salay=? where id=?";
		Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		int m = 0;

		try {
			conn = DbUtil.getConnection();
			st = conn.prepareStatement(sql);
			st.setString(1, person.getpName());
			st.setDate(2, person.getBirthday());
			st.setInt(3, person.getTel());
			st.setInt(4, person.getDepartmentId());
			st.setDouble(5, person.getSalay());
			st.setInt(6, person.getId());

			m = st.executeUpdate();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DbUtil.free(conn, st, rs);
		}

		return m;
	}

	@Override
	public int deletePerson(int id) {

		String sql = "DELETE FROM person WHERE  id =?";
		Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		int m = 0;

		try {
			conn = DbUtil.getConnection();
			st = conn.prepareStatement(sql);
			st.setInt(1, id);
			m = st.executeUpdate();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DbUtil.free(conn, st, rs);
		}

		return m;
	}

	@Override
	public Person loadPerson(int id) {

		String sql = "select * from person where departmentId=2;";
		Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		Person p = null;

		try {
			conn = DbUtil.getConnection();
			st = conn.prepareStatement(sql);
			st.setInt(1, id);
			rs = st.executeQuery();
			if (rs.next()) {
				int pId = rs.getInt("id");
				String pName = rs.getString("pName");
				Date birthday = rs.getDate("birthday");
				int tel = rs.getInt("tel");
				int departmentId = rs.getInt("departmentId");
				double salay = rs.getDouble("salay");

				p = new Person(pName, birthday, tel, departmentId, salay);
				p.setId(pId);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DbUtil.free(conn, st, rs);
		}

		return p;
	}

	@Override
	public List<Person> showAllPerson() {
		String sql = "select * from person";
		Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		List<Person> persons = new ArrayList<Person>();

		try {
			conn = DbUtil.getConnection();
			st = conn.prepareStatement(sql);
			rs = st.executeQuery();
			while (rs.next()) {

				int pId = rs.getInt("id");
				String pName = rs.getString("pName");
				Date birthday = rs.getDate("birthday");
				int tel = rs.getInt("tel");
				int departmentId = rs.getInt("departmentId");
				double salay = rs.getDouble("salay");

				Person p = new Person(pName, birthday, tel, departmentId, salay);
				p.setId(pId);
				persons.add(p);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DbUtil.free(conn, st, rs);
		}

		return persons;
	}

	@Override
	public List<Person> loadPersonByDepartmentId(int did) {
		String sql = "select * from person where departmentId=?";
		Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		List<Person> persons = new ArrayList<Person>();

		try {
			conn = DbUtil.getConnection();
			st = conn.prepareStatement(sql);
			st.setInt(1, did);
			rs = st.executeQuery();
			while (rs.next()) {

				int pId = rs.getInt("id");
				String pName = rs.getString("pName");
				Date birthday = rs.getDate("birthday");
				int tel = rs.getInt("tel");
				int departmentId = rs.getInt("departmentId");
				double salay = rs.getDouble("salay");

				Person p = new Person(pName, birthday, tel, departmentId, salay);
				p.setId(pId);
				persons.add(p);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DbUtil.free(conn, st, rs);
		}

		return persons;
	}

}
